package com.xci.sys.api;

import cn.hutool.extra.servlet.ServletUtil;
import com.xci.core.base.RestMessage;
import com.xci.core.internal.Const;
import com.xci.core.helper.DateHelper;
import com.xci.core.helper.Helper;
import com.xci.core.internal.Sys;
import com.xci.core.server.Server;
import com.xci.core.base.ApiController;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 服务器接口
 * @author 吕艳阳
 */
@ApiSort(1)
@Api(tags = "服务器接口")
@RestController
@RequestMapping(value = "/api/sys/server", produces = Const.P_JSON)
public class ServerApiController extends ApiController {
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "客户端与服务端网络测试")
    @PostMapping("/ping")
    public RestMessage ping() {
        return RestMessage.success(System.currentTimeMillis());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取服务器名称")
    @PostMapping("/name")
    public RestMessage serverName() {
        return RestMessage.success(Sys.setting().getApp().getSysApiServerName());
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取客户端IP地址")
    @PostMapping("/clientIP")
    public RestMessage clientIP() {
        return RestMessage.success(ServletUtil.getClientIP(getRequest()));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取服务器时间")
    @PostMapping("/datetime")
    public RestMessage datetime() {
        return RestMessage.success(DateHelper.format(new Date(), Const.DEFAULT_DATETIME_PATTERN));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "创建新ID")
    @PostMapping("/newId")
    public RestMessage newId() {
        return RestMessage.success(Helper.nextIdStr());
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "创建GUID")
    @PostMapping("/newGuid")
    public RestMessage newGuid() {
        return RestMessage.success(Helper.guid());
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "获取服务器信息")
    @PostMapping("/serverInfo")
    public RestMessage<Server> ServerInfo() {
        Server server = new Server();
        server.init();
        return RestMessage.success(server);
    }
}